package java8NewFeatures.dateDomo;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

/**
 * @author ldy
 * @data 2022/11/10
 * @description * 日期的操纵
 *
 * TemporalAdjuster : 时间校正器。有时我们可能需要获取例如：将日期调整到“下个周日”等操作。
 * TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。
 *
 *
 */
public class TemporalAdjusterAndTemporalAdjusters {
    public static void main(String[] args) {
        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println(localDateTime1);

        // 获取这个月第一天的日期
        System.out.println(localDateTime1.with(TemporalAdjusters.firstDayOfMonth()));
        // 获取下个周末的日期
        System.out.println(localDateTime1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)));

        // 自定义：下一个工作日
        LocalDateTime localDateTime2 = localDateTime1.with(l -> {
            LocalDateTime localDateTime = (LocalDateTime) l;
            DayOfWeek dayOfWeek =  localDateTime.getDayOfWeek();

            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return localDateTime.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return localDateTime.plusDays(2);
            } else {
                return localDateTime.plusDays(1);
            }
        });
        System.out.println(localDateTime2);
        System.out.println(localDateTime2.getYear()+"年"+localDateTime2.getMonth().getValue()+"月"+localDateTime2.getDayOfMonth()+"日");
    }
}
